import javax.jms.*;

public class Ex12_OpenMQReaderListner implements javax.jms.MessageListener
{
    
    public static void main(String [] args) throws Exception
    {
            com.sun.messaging.ConnectionFactory myConnFactory;
            Queue myQueue;
            myConnFactory = new com.sun.messaging.ConnectionFactory();
            Connection myConn = myConnFactory.createConnection();
            Session mySess = myConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
            myQueue = new com.sun.messaging.Queue("MyQueue");

            MessageConsumer myMsgConsumer = mySess.createConsumer(myQueue);
            myMsgConsumer.setMessageListener(new Ex12_OpenMQReaderListner());
            myConn.start();
            System.out.println("waiting for messages ...");
            System.in.read();

            mySess.close();
            myConn.close();
        }
    
    public void onMessage(Message msg)
    {
        try
        {
        if (msg instanceof TextMessage) {
                TextMessage txtMsg = (TextMessage) msg;
                System.out.println("Read Message: " + txtMsg.getText());
            }
        }catch(JMSException ex)
        {
          ex.printStackTrace();
        }
    }
    
}
